//
//  InvestmentGoodsView.m
//  SZBank
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "InvestmentGoodsView.h"
#import "BJSeriesView.h"
#import "BJInvestmentGoodsButton.h"

@interface InvestmentGoodsView ()<ButtonAndTableViewDelegate>
{
    NSMutableDictionary *classifyDictionary;/**< 选项数组 */
    NSMutableArray *detailArray;/**< 详情数组 */
    CGFloat scrWidth;/**< 滚动视图的宽 */
    CGFloat scrHeight;/**< 滚动视图的高 */
}
@property (nonatomic)ButtonAndTableView *goodsButtonTable;/**< 选项视图 */
@end
@implementation InvestmentGoodsView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //数组初始化
        classifyDictionary = [NSMutableDictionary dictionary];
        detailArray = [NSMutableArray array];
        //初始化尺寸
        scrWidth = self.scrollView .frame.size.width;
        scrHeight = self.scrollView.frame.size.height;
  
        //分类网络数据
        [NetWorkingDataManager  getProtocolPreciousMetalsSeriesWithParam:@{@"time":@"2013-01-01 12:40:10"} success:^(id sender) {
            //重组数据
            NSMutableArray *name_array = [NSMutableArray array];
            NSMutableArray *ID_array = [NSMutableArray array];
            for (NSDictionary *dict in sender[@"datalist"]) {
                [name_array addObject:dict[@"NAME"]];
                [ID_array addObject:dict[@"ID"]];
            }
            [classifyDictionary setObject:name_array forKey:@"name"];
            [classifyDictionary setObject:ID_array forKey:@"id"];
            //添加选项栏
            [self addSubview:self.goodsButtonTable];
        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
        //详情网络数据访问
        [NetWorkingDataManager getProtocolPreciousMetalsWithParam:@{@"time":@"2013-01-01 12:40:10"} success:^(id sender) {
            detailArray = [sender[@"datalist"] mutableCopy];

        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
        
        
    }
    return self;
}

#pragma mark -- ButtonAndTableViewDelegate
- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
//    NSLog(@"%@",detailArray);
    
    NSMutableArray *array_ButtonData = [NSMutableArray array];
  
        //组装数据结构
    for (NSDictionary *dict in detailArray) {
        NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];

        if ([dict[@"PRECIOUSMETALSSERIES"] isEqualToString:selectTitle]) {
            [dataDictionary setObject:dict[@"NAME"] forKey:@"name"];
            [dataDictionary setObject:dict[@"THUMBNAILURL"] forKey:@"image"];
            [dataDictionary setObject:dict[@"ID"] forKey:@"id"];
           
        }
        [array_ButtonData addObject:dataDictionary];

    }
    //去除不正确的数据
    if ([selectTitle isEqualToString:@"实物金"]) {
        [array_ButtonData removeObjectAtIndex:0];
    }else if ([selectTitle isEqualToString:@"艺术投资品"]){
        [array_ButtonData removeAllObjects];
    }
    
    NSMutableArray *array =[NSMutableArray array];
    for (NSDictionary *dict in array_ButtonData) {
        if (dict.count != 0 && ![dict[@"name"] isEqualToString:@"TEST修改"]) {
            [array addObject:dict];
        }
    }
    

//如果有数据循环创建button
    
    if (array.count > 0) {
        CGFloat btnW = scrWidth * 0.25;//button宽
        CGFloat btnH = scrHeight * 0.4;//button高
        CGFloat offset = scrWidth * 0.05;//间距
        //先移除原来视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        //创建视图
        for (int i = 0; i <array.count; i ++) {
            BJInvestmentGoodsButton *button = [[BJInvestmentGoodsButton alloc]initWithFrame:CGRectMake(offset + (i % 3) * (btnW + offset), scrHeight * 0.1 + (i / 3) * (btnH + offset), btnW, btnH) withDataDictionary:array[i] orginDataArray:detailArray];
            [self.scrollView addSubview:button];
            //根据button数量确定scroll的回弹大小
            if (array.count > 6) {
                self.scrollView.contentSize = CGSizeMake(scrWidth, array.count * btnH  / 2);
            }else{
                self.scrollView.contentSize = CGSizeMake(scrWidth, scrHeight * 1.2);
            }
      
        }
    }else{
        //移除所有子视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    

}



#pragma mark -- getters
- (ButtonAndTableView *)goodsButtonTable{
    if (!_goodsButtonTable) {
        _goodsButtonTable =  [[ButtonAndTableView alloc]initWithFrame:CGRectMake(0, 0,frameWidth *0.7  , frameHeight * 0.05) dataSource:@{@"title":@"艺术投资品",@"list":classifyDictionary[@"name"]} backImageName:@"理财_下拉框" selectImageName:@"理财_下拉框"];
        _goodsButtonTable.delegate = self;
    }
    return _goodsButtonTable;
}


























@end
